/**
 * Created by hsiacool on 2017/8/18.
 */
'use strict';
import * as Sequelize from 'sequelize';
import {BaseDao} from "./BaseDao";
import {ArticleModel} from "../models/index";
import {ArticleTypeDao} from "./ArticleTypeDao";

let model: Sequelize.Model<Sequelize.Instance<ArticleModel>, ArticleModel> = null;

class ArticleDao extends BaseDao {

    static getModel(): Sequelize.Model<Sequelize.Instance<ArticleModel>, ArticleModel> {
        return model;
    }

    static count(params: any): Promise<number> {
        return new Promise<number>((resolve, reject) => {
            model.count({
                where: params
            }).then((count: number) => {
                resolve(count);
            }).error(reason => {
                reject(reason);
            })
        });
    }

    static find(params: any,offset?:number): Promise<Array<ArticleModel>> {
        return new Promise<Array<ArticleModel>>((resolve, reject) => {
            model.findAll({
                where: params,
                offset:offset,
                limit:10,
                include: [{model: ArticleTypeDao.getModel(), as: "typeModel"}]
            }).then((articles: Array<Sequelize.Instance<ArticleModel>>) => {
                resolve(JSON.parse(JSON.stringify(articles)));
            }).error(reason => {
                reject(reason);
            })
        });
    }

    static update(articleModel: ArticleModel, t?: Sequelize.Transaction): Promise<Array<ArticleModel>> {
        return new Promise<Array<ArticleModel>>((resolve, reject) => {
            model.update(articleModel, {
                transaction: t,
                where: {id:articleModel.id},
            }).then((result: any) => {
                resolve(result)
            }).error(reason => {
                reject(reason);
            })
        });
    }

    static delete(articleModel: ArticleModel, t?: Sequelize.Transaction): Promise<Array<ArticleModel>> {
        return new Promise<Array<ArticleModel>>((resolve, reject) => {
            model.destroy({
                transaction: t,
                where: {id:articleModel.id},
            }).then((result: any) => {
                resolve(result)
            }).error(reason => {
                reject(reason);
            })
        });
    }

    static insert(articleModel:ArticleModel,options?: Sequelize.CreateOptions):Promise<ArticleModel>{
        return new Promise<ArticleModel>((resolve,reject) =>{
            model.create(articleModel,options).then((result:Sequelize.Instance<ArticleModel>)=>{
                resolve(result.toJSON())
            }).error(reason => {
                reject(reason);
            })
        });
    }
}

let schema = {
    ciId:{type:Sequelize.NUMERIC,primaryKey:true,field:"ci_id"},
    cId:{type:Sequelize.NUMERIC,field:"c_id"},
    type:Sequelize.NUMERIC,
    name:Sequelize.STRING,
    content:Sequelize.STRING
};

let modelName: string = "DsArticle";
let tableName: string = "comm_comm_index";


let initDao = function (sequelize: Sequelize.Sequelize, DataTypes:any): Sequelize.Model<Sequelize.Instance<ArticleModel>, ArticleModel> {
    model = sequelize.define<Sequelize.Instance<ArticleModel>, ArticleModel>(modelName, schema, {
        createdAt: false,
        updatedAt: false,
        tableName: tableName,
        classMethods: {
            associate: function () {
                model.hasMany(ArticleTypeDao.getModel(), {foreignKey: "type", as: "typeModel"});
                // model.belongsTo(ArticleTypeDao.getModel(), {targetKey: "c_id",foreignKey: "type", as: "typeModel"});
                // model.belongsToMany(UserDao.getModel(), {through: "org_user", foreignKey: "org_id",as:"userArray"});
            }
        }
    });
    return model;
    // return [modelName,model];
};

export {ArticleDao, initDao, modelName}
